<?php
/**
 * Created by PhpStorm.
 * User: wangshikun
 * Date: 2018/12/27
 * Time: 16:14
 */

namespace code;

use curl\Http;
use pdd\api\PddDdkAllOrderListIncrementGet;
use pdd\api\PddDdkGoodsUnitQuery;
use pdd\api\PddDdkOauthGoodsPidGenerate;
use pdd\api\PddDdkOauthResourceUrlGen;
use pdd\api\PddDdkOauthRpPromUrlGenerate;
use pdd\TopW;
use pdd\api\PddDdkOauthGoodsPromUrlGenerate;
use pdd\api\PddDdkOauthOrderDetailGet;
use pdd\api\PddDdkOauthGoodsZsUnitUrlGen;
use pdd\api\PddDdkOauthGoodsPidQuery;
class PddCode
{
    protected $client_id;
    protected $client_secret;

    public function __construct()
    {
        $config=config('pdd_token_config');
        $this->client_id = $config['client_id'];
        $this->client_secret = $config['client_secret'];

    }

    /**
     * 生成链接
     * @param string $goods_id 商品id
     * @param string $pid  跑单人pid
     * @param string $token 跑单人token
     * @param string $custom_parameters 自定义参数
     * @param string $zs_duo_id 招商id
     * @return mixed $data
     */
    public function getPddUrl($goods_id,$pid,$token,$custom_parameters,$zs_duo_id=''){

        $c=new TopW();
        $c->client_id = $this->client_id;
        $c->client_secret = $this->client_secret;
        $c->access_token=$token;
        $res=new PddDdkOauthGoodsPromUrlGenerate();
        $res->setGoodsIdList($goods_id);
        $res->setPid($pid);
        $res->set_zs_duo_id($zs_duo_id);
        $res->setGenerateShortUrl('true');
        $res->setGenerateWeApp('true');
        $res->set_custom_parameters($custom_parameters);
        $data=$c->execute($res);
        return $data;
    }

    /**
     * 查询指定授权人订单
     * @param $order_sn
     * @param $token
     * @return mixed
     */
	public function orderInfo($order_sn,$token){
		$c=new TopW();
		$c->client_id = $this->client_id;
		$c->client_secret = $this->client_secret;
		$c->access_token=$token;
		$res=new PddDdkOauthOrderDetailGet();
		$res->setorder($order_sn);
		$data=$c->execute($res);
		return $data;
	}

    /**
     * 上传短连接
     * @param $url
     * @param $pid
     * @param $token
     * @return mixed
     */
	public function shortUrl($url,$pid,$token){
		$c=new TopW();
		$c->client_id = $this->client_id;
		$c->client_secret = $this->client_secret;
		$c->access_token=$token;
		$res=new PddDdkOauthGoodsZsUnitUrlGen();
		$res->setpid($pid);
		$res->setsource_url($url);
		$data=$c->execute($res);
		return $data;

	}

    /**
     * 拼多多商品优惠券查询
     * @param $goods_id
     * @param $commander_id
     * @return mixed $request
     */
    public function goodsCount($goods_id,$commander_id){
        $c=new TopW();
        $c->client_id = $this->client_id;
        $c->client_secret = $this->client_secret;
        $rep=new PddDdkGoodsUnitQuery();
        $rep->set_goods_id($goods_id);
        if(!empty($commander_id)){
           $rep->set_zs_duo_id($commander_id);
        }
        $request=$c->execute($rep);
        return $request;


    }

    /**
     * 多多客已生成推广位信息查询
     * @param $token
     * @param $page
     * @param $limit
     * @return array $data
     */

    public function getPddDdkOauthGoodsPidQuery($token,$page=1,$limit=10){
        $c=new TopW();
        $c->client_id = $this->client_id;
        $c->client_secret = $this->client_secret;
        $c->access_token=$token;
        $res=new PddDdkOauthGoodsPidQuery();
        $res->setPage($page);
        $res->setPageSize($limit);
        $data=$c->execute($res);
        return $data;
    }

    /**
     * 设置用户pid
     * @param $name
     * @param $access_token
     * @return array $data
     */
    public function  setPddDdkOauthGoodsPidGenerate($name,$access_token){
        $c=new TopW();
        $c->client_id = $this->client_id;
        $c->client_secret = $this->client_secret;
        $c->access_token=$access_token;
        $res=new PddDdkOauthGoodsPidGenerate();
        $res->setNumber('1');
        $res->setPidNamePist($name);
        $data=$c->execute($res);
        return $data;
    }

    /**
     * 授权刷新
     * @param $refresh_token
     * @return array  $result
     */
    public function pddSessionKey($refresh_token){
        //获取token
        $data=array();
        $data['client_id']=$this->client_id;
        $data['client_secret']=$this->client_secret;
        $data['grant_type']="refresh_token";
        $data['refresh_token']=$refresh_token;
        $content=json_encode($data);
        $url="http://open-api.pinduoduo.com/oauth/token";
        $result=Http::curlPost_json($url,$content,'','Content-Type:application/json');
        $result=json_decode($result,true);
        return $result;
    }

    /**
     * 获取多多客订单
     * @param $set_start_update_time //开始时间;
     * @param $set_end_update_time   //结束时间;
     * @param $page_size  //每页数量;
     * @param $page  //页数;
     * @return mixed  订单数据;
     */
    public function getDdkAllOrderList($set_start_update_time,$set_end_update_time,$page_size,$page){
        $c=new TopW();
        $c->client_id = $this->client_id;
        $c->client_secret = $this->client_secret;
        $res=new PddDdkAllOrderListIncrementGet();
        $res->set_start_update_time($set_start_update_time);//开始时间
        $res->set_end_update_time($set_end_update_time);//结束时间
        $res->page_size($page_size);//每页显示的数量
        $res->page($page);//页数
        $data=$c->execute($res);
        return $data;
    }

    /**
     * 生成营销工具推广链接
     * @param  string $access_token
     * @param  array $data
     * @return array  $data
     */
    public function getPddDdkOauthRpPromUrlGenerate($access_token,$data){
        $c=new TopW();
        $c->client_id = $this->client_id;
        $c->client_secret = $this->client_secret;
        $c->access_token=$access_token;
        $res=new PddDdkOauthRpPromUrlGenerate();
        foreach($data as $k=>$v){
            $res->$k($v);
        }
        $data=$c->execute($res);
        return $data;
    }


    /**
     * 拼多多主站频道推广接口
     * @param string $access_token
     * @param array $data
     * @return array $data
     */
    public function getPddDdkOauthResourceUrlGen($access_token,$data){
        $c=new TopW();
        $c->client_id = $this->client_id;
        $c->client_secret = $this->client_secret;
        $c->access_token=$access_token;
        $res=new PddDdkOauthResourceUrlGen();
        foreach($data as $k=>$v){
            $res->$k($v);
        }
        $data=$c->execute($res);
        return $data;
    }




}